<template>
  <!--  修改密码 -->
  <el-dialog
    width="500px"
    title="修改密码"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserByPlatform } from "@/api/platform/userManage";

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: {},
    },

    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      form: {},
      // 表单校验
      rules: {
        password: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.resetForm();
        Object.keys(this.formData).forEach((key) => {
          this.$set(this.form, key, this.formData[key]);
        });
        this.$set(this.form, 'targetRoleId', this.formData['roleId']);

      }
    },
  },
  methods: {
    handleSubmit() {
      this.$refs["formRef"].validate((valid) => {
        if (valid) {
          updateUserByPlatform(this.form).then((res) => {
            const { code, data, msg } = res;
            if (code === 200) {
              this.$message({ type: 'success', message: msg})
;
              this.cancel();
              this.$emit("getList");
            }
          });
        }
      });
    },
    resetForm() {
      Object.keys(this.form).forEach((key) => (this.form[key] = ""));
    },

    cancel() {
      this.$emit("update:show", false);
    },
  },
};
</script>
<style scoped lang="scss"></style>
